package myList2;

import mylist.IList;
import mylist.MyArrayListEmpty;
import mylist.PosILLegality;

import java.util.Arrays;

public class MyArrayList implements IList2 {
   public int[] elem;
   public int usedSize;
   public static final int DEFAULT_SIZE=10;
   public MyArrayList(){
       this.elem = new int[DEFAULT_SIZE];
   }
   public MyArrayList(int capacity){
       this.elem = new int[capacity];
   }

    @Override
    public void add(int data) {
        checkCapacity();
        this.elem[this.usedSize]=data;
        this.usedSize++;
    }
    private void checkCapacity(){
        if(isFull()){
            //扩容
            elem = Arrays.copyOf(elem,elem.length*2);
        }
    }

    @Override
    public void add(int pos, int data) {
       try{
          checkPosIlleagel(pos);
       }catch (PosILLegality e){
           e.getStackTrace();
           return;
       }
       checkCapacity();
       for(int i = usedSize-1;i>=pos;i--){
           elem[i+1]=elem[i];
       }
       elem[pos] = data;
       usedSize++;

    }
    private void checkPosIlleagel(int pos)throws  PosILLegality{
       if(pos<0&&pos>usedSize){

           throw new PosILLegality("下标不合法"+pos);
       }
    }

    @Override
    public boolean contains(int toFind) {
        if(isEmpty()){
            return false;
        }
        for(int i = 0;i<usedSize;i++){
            if(elem[i]==toFind){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
       if (isEmpty()){
         return  -1;
       }

       for(int i = 0;i<usedSize;i++){
           if(elem[i]==toFind){
               return i;
           }

       }
        return -1;
    }

    @Override
    public int get(int pos)throws PosILLegality {
        checkPosIlleagel(pos);
        if(isEmpty()){
            throw new PosILLegality("下标异常"+pos);
        }
        return elem[pos];
    }

    @Override
    public void set(int pos, int value) {
        checkPosIlleagelAndSet(pos);
        elem[pos] = value;
    }
    private void checkPosIlleagelAndSet(int pos)throws  PosILLegality{
        if(pos<0&&pos>=usedSize){

            throw new PosILLegality("下标不合法"+pos);
        }
    }

    @Override
    public void remove(int toRemove) {
        int index= indexOf(toRemove);
        if(index == -1){
            System.out.println("没有这个数字");
        }
        for(int i = index;i<usedSize-1;i++){
            elem[i] = elem[i+1];
        }
        usedSize--;
    }

    @Override
    public int size() {
        return this.usedSize;
    }

    @Override
    public void clear() {
        this.usedSize=0;
    }

    @Override
    public void display() {
        for(int i = 0;i<usedSize;i++){
            System.out.println(elem[i]+" ");

        }
        System.out.println();
    }

    @Override
    public boolean isFull() {
        return usedSize==elem.length;
    }

    @Override
    public boolean isEmpty() {
        return usedSize==0;
    }
}
